﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using KSUNO_EMS.Models;

namespace KSUNO_EMS.Controllers
{
    public class EmployeeManagementController : EmsController
    {
        //
        // GET: /EmployeeManagment/

        public ActionResult Index()
        {
            return (ActionResult)VerifyAccess("Manager") ?? View();
           // return (ActionResult)VerifyAccess("Manager") ?? View();

        }

        public ActionResult CreateEmployee(EmployeeDTO employee)
        {
            try
            {
                if (!ModelState.IsValid)
                    return new ProperJsonResult() { Data = new AjaxResult(false) };
                var dbEmployee = Mapper.Map<EmployeeDTO, Employee>(employee);
                Database.Employees.Add(dbEmployee);
                Database.SaveChanges();
                return new ProperJsonResult { Data = new AjaxResult(true, dbEmployee) };
            }
            catch (DbEntityValidationException ex)
            {

                throw;
            }

        }

        public ActionResult DeleteEmployee(int id)
        {
            var employee = Database.Employees.SingleOrDefault(e => e.Id == id);
            if (employee != null)
            {
                Database.Employees.Remove(employee);
                Database.SaveChanges();

                return new ProperJsonResult() { Data = new AjaxResult(true) };
            }
            return new ProperJsonResult() { Data = new AjaxResult(false) };
        }

        public ActionResult ListEmployees(Models.Employee employee)
        {
            return new ProperJsonResult() { HasDates = true, Data = new AjaxResult(true, Database.Employees.Include(e => e.Availability).ToList().Select(Mapper.Map<Employee, EmployeeDTO>).ToList()) };
        }

        public ActionResult UpdateEmployee(Models.EmployeeDTO employee)
        {
            if (ModelState.IsValid)
            {

                var dbEmployee = Database.Employees.SingleOrDefault(e => e.Id == employee.Id);
                if (dbEmployee != null)
                {
                    var password = dbEmployee.Password;
                    Mapper.Map(employee, dbEmployee);
                    if (String.IsNullOrEmpty(employee.Password))
                        dbEmployee.Password = password;
                    Database.SaveChanges();
                    return new ProperJsonResult { Data = new AjaxResult(true) };
                }
            }

            return new ProperJsonResult { Data = new AjaxResult(false) };
        }

        public ActionResult UpdateEmployeeAvailability(int employeeId, string availableTimes, string[] test)
        {
            if (ModelState.IsValid)
            {
                var dbEmployee = Database.Employees.Include(e => e.Availability).SingleOrDefault(e => e.Id == employeeId);
                if (dbEmployee != null)
                {
                    foreach (var time in dbEmployee.Availability.ToList())
                        Database.EmployeeAvailabilities.Remove(time);
                    Database.SaveChanges();
                    var times = availableTimes.Deserialise<IEnumerable<EmployeeAvailability>>();
                    foreach (var time in times)
                        dbEmployee.Availability.Add(new EmployeeAvailability { Day = time.Day, From = time.From, To = time.To });

                    Database.SaveChanges();
                    return new ProperJsonResult { Data = new AjaxResult(true) };
                }
            }

            return new ProperJsonResult { Data = new AjaxResult(false) };
        }
    }
}
